//
//  AddressPickerVM.swift
//  kxf
//
//  Created by Mac on 2024/3/16.
//

import Foundation
import UIKit

enum ADDRESS_REGION_TYPE {
    case province
    case city
    case area
}

class AddressPickerVM: UIView {
    
    var selfHeight = kFitWidth(240)
    var regionType = ADDRESS_REGION_TYPE.province
    
    var dataSourceArray = NSArray()
    var selectedDict = NSDictionary()
    
    public var tapBlock:((NSDictionary)->())?
    public var cancelBlock:(()->())?
    
    let pickerView = UIPickerView.init(frame: CGRect.init(x: 0, y: kFitWidth(40), width: SCREEN_WIDHT, height: kFitWidth(200)))

    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x:0, y: 0, width: SCREEN_WIDHT, height: SCREEN_HEIGHT))
        self.backgroundColor = WHColorWithAlpha(colorStr: "000000", alpha: 0.66)
        self.isUserInteractionEnabled = true
        
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var whiteView : UIView = {
        let vi = UIView()
        vi.backgroundColor = .white
        vi.isUserInteractionEnabled = true
        
        return vi
    }()
    lazy var cancelBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("取 消", for: .normal)
        btn.setTitleColor(.COLOR_TEXT_BLACK333 , for: .normal)
        btn.addTarget(self, action: #selector(cancelBtnAction), for: .touchUpInside)
        
        return btn
    }()
    lazy var confirmBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("确 定", for: .normal)
        btn.setTitleColor(.THEME, for: .normal)
        
        btn.addTarget(self, action: #selector(confirmAction), for: .touchUpInside)
        
        return btn
    }()
}

extension AddressPickerVM{
    func setDataSource(dataArray:NSArray){
        self.isHidden = false
        self.dataSourceArray = dataArray
        self.selectedDict = dataArray[0]as? NSDictionary ?? [:]
        self.pickerView.reloadAllComponents()
        self.pickerView.selectRow(0, inComponent: 0, animated: false)
    }
    @objc func cancelBtnAction(){
        self.isHidden = true
        if self.cancelBlock != nil{
            self.cancelBlock!()
        }
    }
    @objc func confirmAction(){
        if self.tapBlock != nil{
            self.tapBlock!(self.selectedDict)
        }
        self.isHidden = true
    }
}


extension AddressPickerVM:UIPickerViewDelegate,UIPickerViewDataSource{
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return self.dataSourceArray.count
    }
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return kFitWidth(40)
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        let dict = self.dataSourceArray[row]as? NSDictionary ?? [:]
        return dict["regionName"]as? String ?? ""
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        self.selectedDict = self.dataSourceArray[row]as? NSDictionary ?? [:]
        DLLog(message: "\(selectedDict)")
    }
}
extension AddressPickerVM{
    func initUI(){
        addSubview(whiteView)
        whiteView.addSubview(pickerView)
        whiteView.addSubview(cancelBtn)
        whiteView.addSubview(confirmBtn)
        
        pickerView.delegate = self
        pickerView.dataSource = self
        
        setConstrait()
    }
    
    func setConstrait(){
        whiteView.snp.makeConstraints { make in
            make.left.width.bottom.equalToSuperview()
            make.height.equalTo(selfHeight+WHBaseViewVC().getBottomSafeAreaHeight())
        }
        cancelBtn.snp.makeConstraints { (frame) in
            frame.left.top.equalToSuperview()
            frame.height.equalTo(kFitWidth(40))
            frame.width.equalTo(kFitWidth(100))
        }
        confirmBtn.snp.makeConstraints { (frame) in
            frame.right.top.equalToSuperview()
            frame.height.equalTo(kFitWidth(40))
            frame.width.equalTo(kFitWidth(100))
        }
    }
}
